/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.zoundry.gaedt.core.ZGaeException;


/**
 * Util class for handling UTF8 encoded data.
 */
public class ZGaeUTF8Util
{
    /** UTF-8 encoding string. */
    public static final String UTF8_ENCODING = "UTF-8"; //$NON-NLS-1$

    /**
     * Convert <code>String</code> to <code>InputStream</code> backed by a
     * UTF-8 encoded byte array.
     *
     * @param aString
     * @throws ZGaeException
     */
    public static InputStream getInputStream(String aString) throws ZGaeException
    {
        return new ByteArrayInputStream(getBytes(aString));
    }

    /**
     * Encodes this <tt>String</tt> into a sequence of bytes using the UTF-8
     * charset, returning the result as a new byte array.
     *
     * @param aString
     * @throws ZGaeException
     */
    public static byte[] getBytes(String aString) throws ZGaeException
    {
        try
        {
            return aString.getBytes(getUTF8Encoding());
        }
        catch (UnsupportedEncodingException io)
        {
            throw new ZGaeException(io);
        }
    }

    /**
     * @return Return the UTF-8 encoding string.
     */
    private static String getUTF8Encoding()
    {
        return UTF8_ENCODING;
    }
}
